package vcp.views;

import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JOptionPane;
import javax.swing.event.AncestorListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.TableColumnModelListener;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableColumnModel;

import client.Message;
import db.parking.beans.rates;
import vcp.common.AbstractView;
import vcp.common.IView;
import vcp.gui.forms.NewRateForm;
import vcp.gui.panels.parkingmanager.PnlRatesManagement;

/**
 *Rates Management View
 *
 */
public class RatesManagementView extends AbstractView implements IView,Observer{
    PnlRatesManagement pnlRatesManagement;
    NewRateForm newRateForm;
    Object controller;
	@SuppressWarnings("unused")

	public RatesManagementView(){
    	pnlRatesManagement = new PnlRatesManagement();
    	newRateForm = pnlRatesManagement.getNewRateForm();
    }	

	public NewRateForm getNewRateForm() {
		// TODO Auto-generated method stub
		return newRateForm;
	}

	public PnlRatesManagement getPnlRatesManagement() {
		return pnlRatesManagement;
	}

	@Override
	public void update(Observable arg0, Object arg) {		
		Message recvmsg = (Message) arg;
		String func = recvmsg.getMsg();
		Object robj=recvmsg.getObj();
		//this.pnlRatesManagement.getRatesTable().getDtm().setRowCount(0);
		if (func.equals("GetRatesByLotNum") && robj!=null ) {			
			this.pnlRatesManagement.getRatesTable().revalidate();
			@SuppressWarnings("unchecked")
			ArrayList<rates> list = 
					(ArrayList<rates>)robj;
			Iterator<rates> itty = list.iterator();
			Object obj[] = new Object[6];
			while (itty.hasNext()) {
				rates r = (rates) itty.next();
				obj[0] = r.getLotNo();
				obj[1] = r.getRateType();
				obj[2] = r.getRateName();
				obj[3] = r.getAmount();
				obj[4] = r.getTmpAmount();
				if(r.getStatus()==1)
				  obj[5] = new Boolean(true);
				else
				  obj[5] = new Boolean(false);
				this.pnlRatesManagement.getRatesTable().getDtm().addRow(obj);
				//this.pnlRatesManagement.getRatesTable().getColumnModel().getColumn(0).setMaxWidth(0);
				//this.pnlRatesManagement.getRatesTable().getColumnModel().getColumn(0).setMinWidth(0);
				
			}
			
			 
		}else if(func.equals("SaveRate")){
			JOptionPane.showMessageDialog(null,
				    recvmsg.getStatusMsg());
		}
	}

	@Override
	public void addController(Object obj) {
		 pnlRatesManagement.getBtnNewRate().addActionListener((ActionListener)obj);
		 pnlRatesManagement.getBtnNewRate().setActionCommand("NewRate");
		 pnlRatesManagement.getBtnEditRate().addActionListener((ActionListener)obj);
		 pnlRatesManagement.getBtnEditRate().setActionCommand("EditRate");
		 pnlRatesManagement.getNewRateForm().getBtnSave().addActionListener((ActionListener)obj);
		 pnlRatesManagement.getNewRateForm().getBtnSave().setActionCommand("SaveRate");
		 pnlRatesManagement.getRatesTable().getDtm().addTableModelListener((TableModelListener)obj);
		 pnlRatesManagement.getRatesTable().addAncestorListener((AncestorListener)obj);
		 this.controller = obj;
		 //pnlRatesManagement.getRatesTable().addMouseListener((MouseListener)obj);
		 //pnlRatesManagement.getRatesTable()
	}
	

}
